/*-*
 * FILENAME  :
 *    $HeadURL$
 *
 * STATUS  :
 *    $Revision$
 *
 *    $Author$
 *    $Date$
 *  
 *    
 * Copyright (c) 2011 Gonalge Ltda. All rights reserved.
 *
 ****************************************************************/

package org.gonalge.rompecabeza.pintado;

import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Area;
import java.awt.geom.RoundRectangle2D;

import org.gonalge.rompecabeza.dto.Pieza;
import org.gonalge.rompecabeza.negocio.ListaPieza;
import org.gonalge.rompecabeza.servidor.FormServidor;
import org.gonalge.rompecabeza.util.FormUtils;

/**
 * 
 * @author Geremias Gonzalez
 */
public class PintarServidor extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = -8709280572297519787L;

    private int ancho, alto, n;

    private final FormServidor servidor;

    private ListaPieza listaPieza = null;

    private int imagen = 1;

    public PintarServidor(FormServidor pS) {
        listaPieza = new ListaPieza();
        this.servidor = pS;
        listaPieza.setXY(15, 5);
        setAnchoAlto(280, 280);
        listaPieza.setNax(140);
        listaPieza.configurar(3, 3);
        ancho = listaPieza.getAncho();
        alto = listaPieza.getAlto();
        n = listaPieza.getN();

    }

    private void setAnchoAlto(int w, int h) {
        listaPieza.setTotalAlto(h);
        listaPieza.setTotalAncho(w);
    }

    public void setFilCol(int f, int c) {
        listaPieza.configurar(f, c);
        ancho = listaPieza.getAncho();
        alto = listaPieza.getAlto();
        n = listaPieza.getN();
        repaint();
    }

    public void setImagen(int img) {
        imagen = img;
        repaint();
    }

    private void pintarMargen(Graphics2D g) {
        g.setColor(Color.BLACK);
        for (int p = 0; p < listaPieza.length(); p++) {
            Pieza pi = listaPieza.getPiesa(p);
            Area a = getArea(pi.getTipo(), pi.getX(), pi.getY());
            g.draw(a);
            a = getArea(pi.getTipo(), pi.getX() - 1, pi.getY());
            g.draw(a);

        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawImage(FormUtils.getImagen(getClass(), "" + imagen), 0, 0, getWidth(), getHeight(), this);

        pintarMargen((Graphics2D) g);

        g.setFont(new Font("Arial", 1, 25));

        g.setColor(FormUtils.getColorTitulo());
        if (servidor.getJuego() != null) {

            if (servidor.getJuego().finJuego())
                g.drawString("JUEGO COMPLETADO", 10, 30);
            else
                g.drawString("JUEGO LANZADO", 40, 30);

        } else
            g.drawString("JUEGO NO LANZADO", 30, 30);

        if (servidor.getJuego() != null) {
            g.setColor(FormUtils.getColorTitulo());
            g.setFont(new Font("Arial", 1, 25));
            g.drawString("Opciones de Juego", 40, 100);
            g.drawString("Fila           : " + servidor.getJuego().getFil(), 60, 130);
            g.drawString("Columna  : " + servidor.getJuego().getCol(), 60, 160);
        }
    }

    private Area cargarfig1(int x, int y) {

        Area a = new Area();

        a.add(new Area(new Rectangle(x + n, y + n, ancho - n, alto - n)));

        a.add(new Area(new RoundRectangle2D.Double(x, y + 3 * n / 2, n * 2 - n / 2, n * 2, n, n)));
        a.subtract(new Area(new RoundRectangle2D.Double(x + ancho - n, y + 3 * n / 2, n * 2, n * 2, n, n)));

        a.add(new Area(new RoundRectangle2D.Double(x + 4 * n / 3, y, n * 2, n * 2 - n / 2, n, n)));
        a.subtract(new Area(new RoundRectangle2D.Double(x + ancho / 2, y + alto - n, n, n * 2, n, n)));

        a.add(new Area(new RoundRectangle2D.Double(x + n, y + alto - n, n, 4 * n / 3, n, n))); // *

        a.subtract(new Area(new RoundRectangle2D.Double(x, y + alto - n, 4 * n / 3, n, n, n)));
        a.add(new Area(new RoundRectangle2D.Double(x + ancho - n, y + alto - n, 4 * n / 3, n, n, n)));

        a.subtract(new Area(new RoundRectangle2D.Double(x + ancho - 4 * n / 3, y, n, n * 2 - n / 2, n, n)));

        return a;
    }

    private Area cargarfig2(int x, int y) {

        Area a = new Area();

        a.add(new Area(new Rectangle(x + n, y + n, ancho - n, alto - n)));

        a.add(new Area(new RoundRectangle2D.Double(x + ancho / 2, y, n, n * 2, n, n)));
        a.subtract(new Area(new RoundRectangle2D.Double(x + 4 * n / 3, y + alto - n, n * 2, n * 2, n, n)));

        a.add(new Area(new RoundRectangle2D.Double(x, y + n, n * 2 - n / 2, n * 2, n, n)));
        a.subtract(new Area(new RoundRectangle2D.Double(x + ancho - n, y + n, n * 2, n * 2, n, n)));

        a.add(new Area(new RoundRectangle2D.Double(x, y + alto - n * 2, n * 2, n, n, n)));
        a.subtract(new Area(new RoundRectangle2D.Double(x + ancho - n, y + alto - n * 2, n * 2, n, n, n)));

        a.subtract(new Area(new RoundRectangle2D.Double(x + n, y, n, 4 * n / 3, n, n))); // *

        a.add(new Area(new RoundRectangle2D.Double(x + ancho - 4 * n / 3, y + alto - n, n, n * 2 - n / 2, n, n)));

        return a;
    }

    public Area getArea(int T, int x, int y) {
        if (T == 1)
            return cargarfig1(x, y);
        else
            return cargarfig2(x, y);
    }
}
